package cn.dankal.basiclib.base.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.TextView;

import com.gyf.immersionbar.ImmersionBar;
import com.qiniu.android.dns.NetworkReceiver;

import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import butterknife.ButterKnife;
import cn.dankal.basiclib.DKUserManager;
import cn.dankal.basiclib.R;
import cn.dankal.basiclib.base.BaseView;
import cn.dankal.basiclib.util.ActivityUtils;
import cn.dankal.basiclib.util.AppUtils;
import cn.dankal.basiclib.util.DKHandler;
import cn.dankal.basiclib.util.TitleBarUtils;
import cn.dankal.basiclib.util.ToastUtils;
import cn.dankal.basiclib.widget.CommonDialog;
import cn.dankal.basiclib.widget.DebugWatermarkText;
import cn.dankal.basiclib.widget.TipDialog;
import cn.dankal.basiclib.widget.statubar.QMUIStatusBarHelper;
import cn.dankal.basiclib.widget.titlebar.ITitleBar;
import cn.dankal.basiclib.widget.titlebar.SingleTextTitle;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * @author Dankal Android Developer
 * @since 2018/7/3
 */

public abstract class BaseActivity extends AppCompatActivity implements BaseView {

    protected TipDialog loadingDialog;
    private CompositeDisposable mDisposables = new CompositeDisposable();
    private NetworkReceiver mReceiver;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getLayoutId() != 0) {
            setContentView(getLayoutId());
//            ImmersionBar.with(this).statusBarDarkFont(true).transparentStatusBar().init();
            //ButterKnife绑定
            ButterKnife.bind(this);

            initStatusBar();
            //初始化组件
            initComponents();

            obtainData();
        }
        initReceiver();

    }

    private void initReceiver() {
        mReceiver = new NetworkReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        registerReceiver(mReceiver, filter);
    }

    //监听网络状态的广播
    private class NetworkReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null && intent.getAction().equals("android.net.conn.CONNECTIVITY_CHANGE")) {
                ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo activeInfo = manager.getActiveNetworkInfo();
                if (null == activeInfo) {
                    notNetWork();
                } else {
                    hasNetWork();
                }
            }
        }
    }

    public void notNetWork(){

    }
    public void hasNetWork(){

    }
    protected void initStatusBar() {
        QMUIStatusBarHelper.setStatusBarLightMode(this);
    }

    public void setTranslucent(Activity context, View view) {
        if (view != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
                view.setPadding(0, QMUIStatusBarHelper.getStatusbarHeight(context), 0, 0);
            }
        }
    }


    /**
     * setContentView(LayoutResId)
     *
     * @return LayoutResId
     */
    @LayoutRes
    protected abstract int getLayoutId();

    /**
     * 初始化组件
     */
    protected abstract void initComponents();

    private TextView tvTitle;

    public void setTitleText(String title){
        if(tvTitle !=null){
            tvTitle.setText(title);
        }
    }
    /**
     * 添加标题栏
     */
    public void addTitleBar(ITitleBar iTitleBar) {
        if (iTitleBar == null) return;

        int titleBarResId = iTitleBar.getViewResId();

        View toolbarContainer = TitleBarUtils.init(this, titleBarResId);

        tvTitle = toolbarContainer.findViewById(R.id.tv_title);
        View viewById = toolbarContainer.findViewById(R.id.iv_onback);
        if (viewById != null) {
            viewById.setOnClickListener(view -> onBackPressed());
        }

        iTitleBar.onBindTitleBar(toolbarContainer);
    }

    public void addSingleTitleBar(String title) {
        addTitleBar(new SingleTextTitle(title));
    }

    public void obtainData() {
    }

    @Override
    public void showLoadingDialog() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
            loadingDialog = null;
        }
        loadingDialog = new TipDialog.Builder(this)
                .setIconType(TipDialog.Builder.ICON_TYPE_LOADING)
                .setTipWord("正在加载")
                .create();
        loadingDialog.show();
    }

    @Override
    public void dismissLoadingDialog() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
    }

    @Override
    public void showToast(String message) {
        try {
            ToastUtils.showShort(message);
        } catch (Exception ignored) {
        }
    }

    @Override
    public void tokenInvalid() {
//      EMClient.getInstance().logout(true);
        CommonDialog.Builder builder = new CommonDialog.Builder(this);
        builder.setTitle("您的飞歌账号在其他设备上通过飞歌密码登录，如果这不是你的操作，你的飞歌密码已经泄露，请修改飞歌密码")
                .setPositiveButton("确定", Color.parseColor("#141414"), type -> {
                    DKUserManager.resetToken();
                    DKUserManager.resetUserInfo();
                    ActivityUtils.finishAllActivities();
                    try {
                        startActivity(new Intent(this,
                                Class.forName(getString(R.string.login_activity_path))));
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                    builder.getDialog().dismiss();
                })
                .create()
                .show();
    }


    @Override
    public void addNetworkRequest(Disposable d) {
        mDisposables.add(d);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mDisposables != null) mDisposables.clear();
        unregisterReceiver(mReceiver);
    }

    @Override
    protected void onPause() {
        ToastUtils.cancel();
        super.onPause();
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            DebugWatermarkText.addDebugTextView(this);
        }
    }

    protected void postDelayedToFinish() {
        new DKHandler(this).postDelayed(new Runnable() {
            @Override
            public void run() {
                finish();
            }
        }, 1000);
    }

    protected void destoryWebview(WebView dkWebView) {
        if (dkWebView != null) {
            ViewGroup parent = (ViewGroup) dkWebView.getParent();
            if (parent != null) {
                parent.removeView(dkWebView);
            }
            dkWebView.removeAllViews();
            dkWebView.destroy();
        }
    }

    @Override
    public void finishActivity() {
        finish();
    }
}
